<?php


namespace app\system\controller;


use app\common\tool\Tool;
use app\system\model\RoleModel;
use think\Controller;

class Role extends Controller
{
    use Tool;
    //角色列表
    public function index(){
        $data = input('');
        //默认
        $pageNow = 1;//当前页
        $pageSize = 10;//一页多少行
        $lineCount = 0;//总记录数
        $pageCount = 0;//总页数
        if(!empty($data)){
            if(isset($data['pageNow'])){
                $pageNow = $data['pageNow'];
            }
            if(isset($data['pageSize'])){
                $pageSize = $data['pageSize'];
            }
        }

        $lineCount = RoleModel::count();
        $pageCount = ceil($lineCount / $pageSize);
        $data = RoleModel::order('id', 'desc')->limit(($pageNow-1) * $pageSize,$pageSize)->select();
        return $this->returnJson(200, '角色列表', [
            'pageNow' => (int)$pageNow,
            'pageSize' => (int)$pageSize,
            'lineCount' => (int)$lineCount,
            'pageCount' => (int)$pageCount,
            'data' => $data,
        ]);
    }

    public function add(){
        $title = input('title');
        $status = input('status');

        if(empty($title)){
            return $this->returnJson(500, '角色名称不能为空');
        }
        if(empty($status)){
            $status = 1;
        }

        if(RoleModel::get(['title' => $title])){
            return $this->returnJson(500, '角色名称已存在');
        }

        $role = RoleModel::create([
            'title' => $title,
            'status' => $status
        ]);
        if(!$role){
            return $this->returnJson(500, '插入数据库失败');
        }
        return $this->returnJson(200, '角色添加成功',$role);
    }

    public function edit(){
        $id = input('id');
        $title = input('title');
        $status = input('status');

        if(empty($title)){
            return $this->returnJson(500, '角色名称不能为空');
        }
        if(empty($status)){
            $status = 1;
        }
        $role = RoleModel::get($id);
        if(!$role){
            return $this->returnJson(500,'该角色不存在');
        }
        $role->title = $title;
        $role->status = $status;
        $role->save();
        return $this->returnJson(200, '角色修改成功', $role);
    }

    public function del(){
        $id = input('id');
        $role = RoleModel::get($id);
        if(!$role){
            return $this->returnJson(500,'该角色不存在');
        }
        $role->delete();
        return $this->returnJson(200,'角色删除成功');
    }

    public function changeStatus(){
        $id = input('id');
        $role = RoleModel::get($id);
        if(!$role){
            return $this->returnJson(500,'该角色不存在');
        }
        $role->status = $role->status == 1 ? 0 : 1;
        $role->save();
        return $this->returnJson(200,'角色状态修改成功',$role);
    }

    public function getNodesByRoleId(){
        $id = input('id');
        if(empty($id)){
            return $this->returnJson(500, '角色id不能为空');
        }
        $role = RoleModel::get($id);
        return $this->returnJson(200, '角色权限节点', $role->Nodes);
    }

    public function role_authorize(){
        $role_id = input('role_id');
        $ids = input('node_ids');

        if(!isset($role_id)){
            return $this->returnJson(500, '角色id不能为空');
        }
        if(!isset($ids)){
            return $this->returnJson(500, '授权节点ids 数组不能为空');
        }
        $role = RoleModel::get($role_id);
        if(!$role){
            return $this->returnJson(500, '角色不存在');
        }
        $role->Nodes()->detach();
        if(!empty($ids)){
            $role->Nodes()->saveAll($ids);
        }
        return $this->returnJson(200, '角色授权编辑成功', $role->Nodes);
    }

}